package com.ll.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ll.auto.JSONAuthentication;
import com.ll.entity.LCart;
import com.ll.entity.LCommodity;
import com.ll.entity.LType;
import com.ll.entity.LUser;
import com.ll.mapper.LCartMapper;
import com.ll.mapper.LCommodityMapper;
import com.ll.mapper.LUserMapper;
import com.ll.response.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ll
 * @since 2022-03-15
 */
@RestController
@RequestMapping("/l-cart")
public class LCartController extends JSONAuthentication {
    @Autowired
    LCartMapper lCartMapper;
    @Autowired
    LUserMapper lUserMapper;
    @Autowired
    LCommodityMapper lCommodityMapper;
    @ApiOperation(value = "添加订单")
    @PostMapping("/add")
    public void addUser(HttpServletRequest request, HttpServletResponse response, @RequestBody LCart lCart) throws Exception {

            if (lCartMapper.insert(lCart) > 0) {
                Result ok = Result.ok().message("下单成功");
                this.WriteJSON(request, response, ok);
            } else {
                Result error = Result.error().message("下单失败");
                this.WriteJSON(request, response, error);
            }

    }
    @ApiOperation(value = "删除订单")
    @PostMapping("/del")
    public void delUser(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id) throws Exception {
        int i = lCartMapper.deleteById(id);
        if (i > 0) {
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "付款订单")
    @PostMapping("/updOrder")
    public void updFoodPrice(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int  id) throws Exception {
        LCart lCart=lCartMapper.selectById(id);
        LUser lUser=lUserMapper.selectById(lCart.getUserId());
        float chiyou=lUser.getBalance();
        float xuyao=lCart.getPrice();
        if(chiyou-xuyao>0){
            lCart.setStatus("已付款");
            lUser.setBalance(chiyou-xuyao);
            if (lCartMapper.updateById(lCart) > 0&&lUserMapper.updateById(lUser)>0) {
                Result ok = Result.ok();
                this.WriteJSON(request, response, ok);
            } else {
                Result error = Result.error();
                this.WriteJSON(request, response, error);
            }
        }else{
            Result error = Result.error().message("余额不足");
            this.WriteJSON(request, response, error);
        }

    }
    @ApiOperation(value = "发货订单")
    @PostMapping("/updOrderFh")
    public void updOrderFh(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id) throws Exception {
       LCart lCart= lCartMapper.selectById(id);
        lCart.setStatus("已发货");
        if (lCartMapper.updateById(lCart) > 0) {
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "签收订单")
    @PostMapping("/updOrderQs")
    public void updOrderQs(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") int id) throws Exception {
        LCart lCart= lCartMapper.selectById(id);
        lCart.setStatus("已签收");
        if (lCartMapper.updateById(lCart) > 0) {
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }
    @ApiOperation(value = "查找全部已支付订单")
    @PostMapping("/findAllOrderZF")
    public void findAllOrderZF(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int size, @RequestParam("current") int current) throws Exception {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("status","待发货");
        Page<LCart> page = new Page<>(current, size);
        Page<LCart> pages = lCartMapper.selectPage(page, wrapper);
        List<LCart> list = pages.getRecords();
        int total = lCartMapper.selectCount(wrapper);
        for(LCart a:list){
            a.setLUser(lUserMapper.selectById(a.getUserId()));
            a.setLCommodity(lCommodityMapper.selectById(a.getComId()));
        }
        if (list.size() > 0) {
            Result ok = Result.ok().data("order", list).data("total", total);
            this.WriteJSON(request, response, ok);
        }
        Result error = Result.error();
        error.setMessage("没有订单");
        this.WriteJSON(request, response, error);
    }
    @ApiOperation(value = "查找全部已发货订单")
    @PostMapping("/findAllOrderFH")
    public void findAllOrderFH(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int size, @RequestParam("current") int current) throws Exception {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("status","已发货");
        Page<LCart> page = new Page<>(current, size);
        Page<LCart> pages = lCartMapper.selectPage(page, wrapper);
        List<LCart> list = pages.getRecords();
        int total = lCartMapper.selectCount(wrapper);
        for(LCart a:list){
            a.setLUser(lUserMapper.selectById(a.getUserId()));
            a.setLCommodity(lCommodityMapper.selectById(a.getComId()));
        }
        if (list.size() > 0) {
            Result ok = Result.ok().data("order", list).data("total", total);
            this.WriteJSON(request, response, ok);
        }
        Result error = Result.error();
        error.setMessage("没有订单");
        this.WriteJSON(request, response, error);
    }
    @ApiOperation(value = "根据用户id查询不是未支付的订单")
    @PostMapping("/findAllOrderNoZf")
    public void findAllOrderNoZf(HttpServletRequest request, HttpServletResponse response, @RequestParam("size") int size, @RequestParam("current") int current,@RequestParam("userId") int userId) throws Exception {
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.ne("status","未发货");
        wrapper.eq("user_id",userId);
        Page<LCart> page = new Page<>(current, size);
        Page<LCart> pages = lCartMapper.selectPage(page, wrapper);
        List<LCart> list = pages.getRecords();
        int total = lCartMapper.selectCount(wrapper);
        for(LCart a:list){
            a.setLUser(lUserMapper.selectById(a.getUserId()));
            a.setLCommodity(lCommodityMapper.selectById(a.getComId()));
        }
        if (list.size() > 0) {
            Result ok = Result.ok().data("order", list).data("total", total);
            this.WriteJSON(request, response, ok);
        }
        Result error = Result.error();
        error.setMessage("没有订单");
        this.WriteJSON(request, response, error);
    }
}

